/* eslint-disable */
import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "@/views/layout";

// import Login from '@/views/login'
// import Register from '@/views/register'
// import Detail from '@/views/detail'
// import Article from '@/views/article'
// import Collect from '@/views/collect'
// import Like from '@/views/like'
// import User from '@/views/user'
// import Apply from "@/views/user";

import { getToken } from "@/utils/storage";
import { Toast } from "vant";

const Login = () => import("@/views/login");
const Detail = () => import("@/views/detail");
const Register = () => import("@/views/register");
const Article = () => import("@/views/article");
const Collect = () => import("@/views/collect");
const Record = () => import("@/views/record");
const User = () => import("@/views/user");
const Apply = () => import("@/views/apply");
const Infomation = () => import("@/views/infomation");
const Home = () => import("@/views/home");
const ApplyDetail = () => import("@/views/apply-detail");
const Car = () => import("@/views/car");
const Address = () => import("@/views/address");
const NotFound404 = () => import("@/views/not-found404.vue");
const RecordDetail = () => import("@/views/record-detail");
const Recommend = () => import("@/views/recommend");
const VuexDemo = () => import("@/views/vuexDemo.vue");
const RecommendAdd = () => import("@/views/recommend-add.vue");
const RecommendEdit = () => import("@/views/recommend-edit.vue");
const Passcode = () => import("@/views/passcode.vue");
const UserCenter = () => import("@/views/user-center.vue");
const RecommendSelect = () => import("@/views/recommend-select.vue");

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    { path: "/", redirect: "/home" },
    {
      path: "/layout",
      component: Layout,
      children: [
        { path: "/home", component: Home },
        { path: "/apply", component: Apply },
        { path: "/infomation", component: Infomation },
        { path: "/article", component: Article },
        { path: "/collect", component: Collect },
        { path: "/record", component: Record },
        { path: "/user", component: User },
        { path: "/apply-detail", component: ApplyDetail },
        { path: "/car", component: Car },
        { path: "/address", component: Address },
        { path: "/record-detail", component: RecordDetail },
        { path: "/recommend", component: Recommend },
        { path: "/recommend-add", component: RecommendAdd },
        { path: "/recommend-edit", component: RecommendEdit },
        { path: "/apply", component: Apply },
        { path: "/passcode", component: Passcode },
        { path: "/user-center", component: UserCenter },
        { path: "/recommend-select", component: RecommendSelect },
      ],
    },
    { path: "/register", component: Register },
    { path: "/detail", component: Detail },
    { path: "/login", component: Login },
    { path: "/vuex-demo", component: VuexDemo },
    { path: "/*", component: NotFound404 },
  ],
});

router.beforeEach((to, from, next) => {
  const whiteList = ["/login", "/register"];
  // console.log(getToken())
  const token = getToken();
  // console.log(token)
  if (token) {
    next();
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      Toast.fail("很抱歉，您没有访问权限，将前往登录页");
      next("login");
    }
  }
});

export default router;
